package com.xiaoma.tpo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.TUserRoleDao;
import com.xiaoma.tpo.domain.TUserRole;
import com.xiaoma.tpo.dto.TUserRoleVO;

@Service
public class TUserRoleService {
	@Autowired
	private TUserRoleDao tUserRoleDao;
	
	//保存userId 和 roleId 的关系
	public void save(TUserRoleVO tUserRoleVO){
		//先删除user下的所有角色
		tUserRoleDao.delete(tUserRoleVO.getUserId());
		for(Integer roleId:tUserRoleVO.getRoleIds()){
			TUserRole tUserRole = new TUserRole();
			tUserRole.setUserId(tUserRoleVO.getUserId());
			tUserRole.setRoleId(roleId);
			tUserRoleDao.insert(tUserRole);
		}
	}
	//根据userId查询roleId
	public List<TUserRole> find(Integer userId){
		TUserRole tUserRole = new TUserRole();
		tUserRole.setUserId(userId);
		List<TUserRole> rTUserRoles = tUserRoleDao.findByColumns(tUserRole);
		return rTUserRoles;
	}
	//根据userid删除关联关系
	public void delete(Integer userId) {
		tUserRoleDao.delete(userId);
	}
}
